package com.lianheng.cms.common.gen;

import com.jfinal.plugin.activerecord.generator.ColumnMeta;
import com.jfinal.plugin.activerecord.generator.MetaBuilder;
import com.jfinal.plugin.activerecord.generator.TableMeta;
import com.lianheng.cms.common.GenAppTest;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Set;

public class MetaBuilderSwagger extends MetaBuilder {

    String[] tableArray = {
            "t_department"
    };

    public MetaBuilderSwagger(DataSource dataSource) {

        super(dataSource);
    }

    /**
     * 此处填入需要生成model的表
     **/
    @Override
    protected boolean isSkipTable(String tableName) {

//        for (int i = 0; i < tableArray.length; i++) {
//            if (tableName.equals(tableArray[i])) {
//                return false;
//            }
//        }
        Set<String> tables = GenAppTest.tablearray.keySet();
        for (String table : tables) {
            if (tableName.equals(table)) {
                return false;
            }
        }
        return true;
    }

    @Override
    protected String buildBaseModelName(String modelName) {
        return modelName + "Dto";
    }

    //TableMeta对应.jf模板中的类型与属性
    @Override
    protected void buildColumnMetas(TableMeta tableMeta) throws SQLException {
        String remarks = tableMeta.remarks;
        System.err.println(remarks);
        String sql = this.dialect.forTableBuilderDoBuild(tableMeta.name);
        Statement stm = this.conn.createStatement();
        ResultSet rs = stm.executeQuery(sql);
        ResultSetMetaData rsmd = rs.getMetaData();
        for (int i = 1; i <= rsmd.getColumnCount(); ++i) {

            ColumnMeta cm = new ColumnMeta();
            //获取Mysql字段注释
            cm.name = rsmd.getColumnName(i);
            rs = dbMeta.getColumns(conn.getCatalog(), (String) null, tableMeta.name, cm.name);
            rs.next();
            cm.remarks = rs.getString("REMARKS");


            String typeStr = null;
            int type;
            if (this.dialect.isKeepByteAndShort()) {
                type = rsmd.getColumnType(i);
                if (type == -6) {
                    typeStr = "Byte";
                } else if (type == 5) {
                    typeStr = "Short";
                }
            }

            if (typeStr == null) {
                String colClassName = rsmd.getColumnClassName(i);
                typeStr = this.typeMapping.getType(colClassName);
            }

            if (typeStr == null) {
                type = rsmd.getColumnType(i);
                if (type != -2 && type != -3 && type != -4 && type != 2004) {
                    if (type != 2005 && type != 2011) {
                        if (type != 93 && type != 91) {
                            if (type == 1111) {
                                typeStr = "Object";
                            } else {
                                typeStr = "String";
                            }
                        } else {
                            typeStr = "Date";
                        }
                    } else {
                        typeStr = "String";
                    }
                } else {
                    typeStr = "byte[]";
                }
            }

            typeStr = this.handleJavaType(typeStr, rsmd, i);
            cm.javaType = typeStr;
            cm.attrName = this.buildAttrName(cm.name);
            tableMeta.columnMetas.add(cm);
        }

        rs.close();
        stm.close();
    }


}
